//
//  EnterPasswordView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/7/4.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "EnterPasswordView.h"

#import "PasswordWindow.h"//密码框

@implementation EnterPasswordView
{
    PasswordWindow *_window;
    
    NSArray *_codeImageArray;//密码图片数组
    
    NSTimer *_time;//延时定时器
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setNeedsLayout];
    }
    return self;
}

- (void)setNeedsLayout{
    //背景透明黑
    UIButton *background = [UIButton buttonWithType:UIButtonTypeCustom];
    background.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    background.backgroundColor = [UIColor blackColor];
    background.alpha = 0.3;
    [background addTarget:self action:@selector(hiddenEnterPwdWindow) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:background];
    
    //白色View
    _windowView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 180, SCREEN_WIDTH, 180)];
    _windowView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_windowView];
    
    //X
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    close.frame = CGRectMake(15, 15, 20, 20);
    [close setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [close addTarget:self action:@selector(closeWindow) forControlEvents:UIControlEventTouchUpInside];
    [_windowView addSubview:close];
    
    //输入密码
    UILabel *enterPwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 70, 0, 140, 50)];
    enterPwdLabel.text = @"输入密码";
    enterPwdLabel.font = [UIFont systemFontOfSize:20];
    enterPwdLabel.textAlignment = NSTextAlignmentCenter;
    [_windowView addSubview:enterPwdLabel];
    
    //分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [_windowView addSubview:line];
    
    //密码输入框
    _window = [[[NSBundle mainBundle] loadNibNamed:@"PasswordWindow" owner:nil options:nil] lastObject];
    _window.frame = CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_WIDTH / 5);
    [_window.password addTarget:self  action:@selector(valueChanged:)  forControlEvents:UIControlEventAllEditingEvents];
    [_window.password becomeFirstResponder];
    
    //将密码ImageView加入到数组中
    _codeImageArray = [NSArray arrayWithObjects:_window.firstCode, _window.secondCode, _window.thirdCode, _window.fourthCode, _window.fifthCode, _window.sixthCode, nil];
    [_windowView addSubview:_window];
    
    //忘记密码
    UIButton *forgetPwd = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetPwd.frame = CGRectMake(SCREEN_WIDTH - 100, _window.frame.origin.y + _window.frame.size.height, 90, 25);
    [forgetPwd setTitle:@"忘记密码?" forState:UIControlStateNormal];
    forgetPwd.titleLabel.font = [UIFont systemFontOfSize:15];
    forgetPwd.titleLabel.textAlignment = NSTextAlignmentRight;
    [forgetPwd setTitleColor:[UIColor colorWithRed:100/255.0 green:200/255.0 blue:236/255.0 alpha:1] forState:UIControlStateNormal];
    [forgetPwd addTarget:self action:@selector(forgetPassword) forControlEvents:UIControlEventTouchUpInside];
    [_windowView addSubview:forgetPwd];
}

//输入一位时,显示一位code图片
- (void)valueChanged:(UITextField *)textField{
    int count = (int)textField.text.length - 1;
    if (count < 6) {
        for (int i = 0; i < 6; i++) {
            UIImageView *codeImageView = _codeImageArray[i];
            if (i <= count) {
                codeImageView.hidden = NO;
            }else codeImageView.hidden = YES;
        }
    }
    if (textField.text.length == 6) {
        //延时0.3s
        _time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(delay) userInfo:nil repeats:NO];
        return;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self hiddenEnterPwdWindow];
    return YES;
}
//隐藏出入密码框
- (void)hiddenEnterPwdWindow{
    _windowClick(@"hidden");
}
//close
- (void)closeWindow{
    _windowClick(@"close");
}
//忘记密码
- (void)forgetPassword{
    _windowClick(@"forget");
}
//延时
- (void)delay{
    //传密码
    NSString *pwd = [NSString stringWithFormat:@"%@", _window.password.text];
    _windowClick(pwd);
    //定时器销毁
    [_time invalidate];
}

@end
